package com.waimai1.waimai.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.aliyun.tea.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.waimai1.waimai.dao.MyOrderDao;
import com.waimai1.waimai.entity.LocalCoupon;
import com.waimai1.waimai.entity.LocalGood;
import com.waimai1.waimai.entity.MyOrder;
import com.waimai1.waimai.exception.EnumException;
import com.waimai1.waimai.exception.NotErrorException;
import com.waimai1.waimai.service.LocalCouponService;
import com.waimai1.waimai.service.LocalGoodService;
import com.waimai1.waimai.service.MyOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * (MyOrder)表服务实现类
 *
 * @author makejava
 * @since 2023-10-03 16:16:20
 */
@Service("myOrderService")
public class MyOrderServiceImpl extends ServiceImpl<MyOrderDao, MyOrder> implements MyOrderService {
    @Autowired
    private LocalGoodService localGoodService;
    @Autowired
    private LocalCouponService localCouponService;

    @Override
    public Double getLocalPrice(String comments) {
        List<Object> goods = JSON.parseArray(comments);
        Double totalPrice = 0.00;
        Double couponPrice = 0.00;
//        分优惠计算总价，priceMap包含所有优惠对应的价格
        Map<String, Double> priceObj = new HashMap<>();
        for (Object item : goods) {
            Map<String, Object> good = (Map<String, Object>) item;
            LocalGood localGood = localGoodService.getById((Integer) good.get("id"));
            if (null == localGood) {
                throw new NotErrorException(EnumException.NO_GOOD);
            }
            Integer number = (Integer) good.get("number");
            Integer specIndex = (Integer) good.get("specIndex");
            ArrayList<Map<String, Object>> specPrice=new ArrayList<>();
            JSONArray jsonArray=JSONArray.parseArray(localGood.getSpecPrice());
            for(int i=0;i<jsonArray.size();i++){
                specPrice.add((Map<String, Object>)jsonArray.get(i)) ;
            }
            Double mapPrice = number * Double.parseDouble(specPrice.get(specIndex).get("price").toString()) ;
            if (StringUtils.isEmpty(localGood.getLocalCouponNo())) {
                totalPrice = totalPrice + mapPrice;
                break;
            }
            if (priceObj.containsKey(localGood.getLocalCouponNo())) {
                priceObj.put(localGood.getLocalCouponNo(), priceObj.get(localGood.getLocalCouponNo()) + mapPrice);
            } else {
                priceObj.put(localGood.getLocalCouponNo(), mapPrice);
            }
        }
//          计算优惠数组里的优惠，达到门槛抵扣
        LambdaQueryWrapper<LocalCoupon> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        for (Map.Entry<String, Double> entry : priceObj.entrySet()) {
            System.out.println(entry.getKey() + "--->" + entry.getValue());
            lambdaQueryWrapper.eq(LocalCoupon::getCouponNo, entry.getKey());
            LocalCoupon coupon = localCouponService.getOne(lambdaQueryWrapper, false);
            if (null != coupon) {
                //判断券有效
                if (coupon.getStartTime().getTime() < new Date().getTime() && coupon.getEndTime().getTime() > new Date().getTime() && coupon.getSendNumber() < coupon.getNumber()) {
//                    判断是否达到优惠门槛
                    if (entry.getValue() > coupon.getNeedCount()) {
                        couponPrice = couponPrice + coupon.getYouCount();
                    }
                }
            }
            totalPrice = totalPrice + entry.getValue();
        }
        return totalPrice-couponPrice;
    }
}

